package com.trainoo.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

/**
 * @author jackass
 *
 */
@Configuration
@EnableApolloConfig("genlot.public-redis")
public class CommonRedisConfiguration {

	@Bean(name = "redisTemplate")
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFacotry) {
		RedisTemplate<String, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFacotry);
		// 使用fastjson序列化
		FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
		// value值的序列化采用fastJsonRedisSerializer
		template.setValueSerializer(fastJsonRedisSerializer);
		template.setHashValueSerializer(fastJsonRedisSerializer);
		// key的序列化采用StringRedisSerializer
		template.setKeySerializer(new StringRedisSerializer());
		template.setHashKeySerializer(new StringRedisSerializer());
		return template;
	}

	private RedisStandaloneConfiguration getStandaloneConfig(RedisProperties properties){
		RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
		redisConfig.setHostName(properties.getHostName());
		redisConfig.setPort(properties.getPort());
		// redisConfig.setPassword(RedisPassword.of("AGd%l$S#Xy!E7a73amdicQ90cZ1"));
		redisConfig.setDatabase(properties.getDatabase());
		return redisConfig;
	}

	private RedisConnectionFactory getConnectionFactory(RedisStandaloneConfiguration redisConfig){
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(100);
		jedisPoolConfig.setMaxIdle(10);
		jedisPoolConfig.setNumTestsPerEvictionRun(1024);
		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
		jedisPoolConfig.setMinEvictableIdleTimeMillis(1800000);
		jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000);
		jedisPoolConfig.setMaxWaitMillis(2000);
		jedisPoolConfig.setTestOnBorrow(true);
		jedisPoolConfig.setTestWhileIdle(true);
		jedisPoolConfig.setBlockWhenExhausted(false);

		JedisClientConfiguration.DefaultJedisClientConfigurationBuilder configurationBuilder =
				(JedisClientConfiguration.DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();

		/** 1. 使用连接池需要这样配置 */
		configurationBuilder.usePooling().poolConfig(jedisPoolConfig);
		/** 2. 注：下面配置并没有使用到连接池 */
		//configurationBuilder.poolConfig(jedisPoolConfig);
		configurationBuilder.readTimeout(Duration.ofMillis(1000));
		configurationBuilder.connectTimeout(Duration.ofMillis(1000));
		JedisClientConfiguration clientConfiguration = configurationBuilder.build();
		RedisConnectionFactory redisConnectionFacotry = new JedisConnectionFactory(redisConfig, clientConfiguration);
		return redisConnectionFacotry;
	}
}
